package com.audaque.springboot.foshanupload.web.esdatademo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CloseIndexRequest;
import org.elasticsearch.client.indices.CloseIndexResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zgb
 * @desc ...
 * @date 2023-06-12 20:43:09
 */
@Api(tags = "增删改查（索引）")
@RestController
@RequestMapping("restHighLevelClientIndex")
public class RestHighLevelClientIndexController {
    @Autowired
    RestHighLevelClient restHighLevelClient;

    @ApiOperation("创建索引")
    @GetMapping("creatIndex")
    public void creatIndex() throws Exception {
        GetIndexRequest blog1 = new GetIndexRequest("blog");
        boolean exists = restHighLevelClient.indices().exists(blog1, RequestOptions.DEFAULT);
        // 1.删除已经存在的索引
        if(exists){
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
            restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        }
        // 2.创建一个索引
        CreateIndexRequest blog = new CreateIndexRequest("blog");
        // 3.配置 settings
        blog.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1));
        // 4.配置字段类型，字段类型可以通过 JSON 字符串、Map 以及 XContentBuilder 三种方式来构建
        // 4.1.json 字符串的方式
        blog.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
        // 5.执行请求，创建索引
        restHighLevelClient.indices().create(blog, RequestOptions.DEFAULT);


    }


    @ApiOperation("创建嵌套索引")
    @GetMapping("creatIndexNested")
    public void creatIndexNested() throws Exception {
        // 1.删除已经存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user");
        CreateIndexRequest request = new CreateIndexRequest("user");
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("properties");
            {
                builder.startObject("children");
                {
                    builder.field("type", "nested");
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        request.mapping(builder);
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);


    }


    @ApiOperation("索引是否存在")
    @GetMapping("exists")
    public void exists() throws Exception {
        GetIndexRequest blog = new GetIndexRequest("blog");
        boolean exists = restHighLevelClient.indices().exists(blog, RequestOptions.DEFAULT);
        System.out.println("exists:" + exists);


    }


    @ApiOperation("打开/关闭索引")
    @GetMapping("closeOropen")
    public void closeOropen() throws Exception {
        // 关闭索引
        CloseIndexRequest blog = new CloseIndexRequest("blog");
        CloseIndexResponse close = restHighLevelClient.indices().close(blog, RequestOptions.DEFAULT);
        List<CloseIndexResponse.IndexResult> indices = close.getIndices();
        for (CloseIndexResponse.IndexResult index : indices) {
            System.out.println("index.getIndex()" + index.getIndex());
        }
        // 打开索引
        OpenIndexRequest blog1 = new OpenIndexRequest("blog");
        OpenIndexResponse open = restHighLevelClient.indices().open(blog1, RequestOptions.DEFAULT);


    }


    @ApiOperation("修改索引")
    @GetMapping("putSettings")
    public void putSettings() throws Exception {
        UpdateSettingsRequest update = new UpdateSettingsRequest("blog");
        update.settings(Settings.builder().put("index.blocks.write", true).build()); // 禁用write
        restHighLevelClient.indices().putSettings(update, RequestOptions.DEFAULT);


    }


    @ApiOperation("查看索引信息")
    @GetMapping("getSettings")
    public void getSettings() throws Exception {
        GetSettingsRequest request = new GetSettingsRequest().indices("blog"); // 支持数组参数，获取多个索引信息
        // 设置需要获取的具体参数名，不设置则返回所有参数
        request.names("index.blocks.write");
        GetSettingsResponse response = restHighLevelClient.indices().getSettings(request, RequestOptions.DEFAULT);
        ImmutableOpenMap<String, Settings> settings = response.getIndexToSettings();
        System.out.println(settings);
        // 查看某个索引中的参数
        String s = response.getSetting("blog", "index.number_of_replicas");
        System.out.println(s);


    }
}
